//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Type name tab expansion
//
// Creation Date: Jan 8, 2007
//
//---------------------------------------------------------------------
using System;
using System.Text.RegularExpressions;

using BGShell.PowerShell.Commands;
using BGShell.PowerShell.TabExpansion.Caches;

namespace BGShell.PowerShell.TabExpansion.Handlers
{
    internal sealed class TypeNameHandler : TabExpansionHandler
    {
        public TypeNameHandler(GetTabExpansionCommand command)
            : base(command)
        {
        }

        public override bool TryExpand(String line, String word)
        {
            Match m = _patternRemoveBraces.Match(word);

            if (m.Success)
            {
                WriteExpansions(m.Groups["TypeName"].Value);
                return false;
            }

            m = _pattern.Match(word);

            if (m.Groups["ClosingBracket"].Success)
            {
                WriteExpansions(word);
                return false;
            }
            else if (m.Success)
            {
                WriteExpansions(TypeNameCache.Instance.Expand(m.Groups["TypeName"].Value));
                return false;
            }

            return true;
        }

        private static readonly Regex _patternRemoveBraces = new Regex(@"(?x)
            \[ (?<TypeName> [^\]]* ) \]=");

        private static readonly Regex _pattern = new Regex(@"(?x)
            \[ (?<TypeName> [^\]]*) (?<ClosingBracket> \])? $");
    }
}
